// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
// import Vue from 'vue'
import App from './App'
import router from './router'
import CryptoJS from 'crypto-js'
import Vant from 'vant';
import 'vant/lib/index.css';
Vue.use(Vant);

// import 'element-ui/lib/theme-chalk/index.css';
// import ElementUI from 'element-ui';
// Vue.use(ElementUI);

import VueParticles from 'vue-particles'
Vue.use(VueParticles)
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)

import VueCropper from 'vue-cropper'
Vue.use(VueCropper)

import axios from 'axios'
Vue.use(axios)
Vue.prototype.axios = axios

import VueJsonp from 'vue-jsonp'
Vue.use(VueJsonp)

import peizhis from '@/assets/peizhi.js'
Vue.prototype.peizhis = peizhis


Vue.config.productionTip = false

import * as echarts from "echarts"
Vue.prototype.$echarts = echarts

Vue.prototype.allurl = window.location.origin+"/91haoka_platform/"


// axios.interceptors.request.use(function (config) {
//   const token = JSON.parse(localStorage.getItem('USER'))['X-Login-Token'] ;
//   console.log(token)
//   if(token){
//     config.headers['X-XSRF-TOKEN'] = token
//   }
//   return config;
// }, function (error) {
//   return Promise.reject(error);
// });


axios.interceptors.response.use(
	response => {
		if (response.status === 200) {
			if(response.data.msg && response.data.msg.code != 0){
				if(response.data.msg.code == 50401){
					router.push({path:'/login'})
				}else{
					return Promise.resolve(response);
				}
			}else{
				return Promise.resolve(response);
			}
		} else {

			return Promise.reject(response);
		}
	},
)

//公共方法go
Vue.prototype.goid = function(url,id){
    router.push({
		path:url,
		query:{
			id:id
		}
	})
}


Vue.prototype.checkquanxian = function(what){
    let user = JSON.parse(localStorage.getItem('USER')).functions
    return user.filter(item=>item.code == what)
}

Vue.prototype.checkkong = function(what){
    if(what && what != null && what != undefined && what != '' && what.length > 0 ){
        return false
    }else{
        return true
    }
}




Vue.prototype.back = function(){
    router.back(-1)
},

Vue.prototype.goopen = function(url){
  window.open(window.location.origin + window.location.pathname + '#/' + url)
}

const keyStr = "ofGlyFgu9bL2J7VQ"
Vue.prototype.decrypt = function(word){
  var key  = CryptoJS.enc.Utf8.parse(keyStr);//Latin1 w8m31+Yy/Nw6thPsMpO5fg==
  var decrypt = CryptoJS.AES.decrypt(word, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7},1);
  return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}

Vue.prototype.resetSetItem = function (key, newVal) {
  if (key === 'watchStorage') {

      // 创建一个StorageEvent事件
      var newStorageEvent = document.createEvent('StorageEvent');
      const storage = {
          setItem: function (k, val) {
              sessionStorage.setItem(k, val);

              // 初始化创建的事件
              newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);

              // 派发对象
              window.dispatchEvent(newStorageEvent)
          }
      }
      return storage.setItem(key, newVal);
  }
}





router.beforeEach((to, from, next) => {

  // let person = JSON.parse(localStorage.getItem('USER'))
  // if(person.functions != ''){
  //   person.functions.forEach(item=>{
  //     if(item.code == 'S'){
  //       let routerObj = {
  //           path: '/xinbaobiao',
  //           name: 'xinbaobiao',
  //           component: () => import('./views/baobiao/index')
  //       }
  //       console.log(routerObj)
  //       router.addRoute(routerObj)
  //     }
  //   })
  // }


  let p = new Promise((res,rej)=>{
    let moban = localStorage.getItem('moban')
    if(moban){
      Vue.prototype.moban = JSON.parse(moban)
      res('ok')
    }else{
      axios.get(`https://file.storep.91haoka.cn/taowa/${location.hostname}.json?v=${Math.random()}`)
        	.then(response => {
            Vue.prototype.moban = JSON.parse(response.data)
            localStorage.setItem('moban',response.data)
            res('ok')
        	})
          .catch(error=>{
            axios.get(`https://file.storep.91haoka.cn/taowa/test.91haoka.cn.json?v=${Math.random()}`)
            .then(response => {
              Vue.prototype.moban = JSON.parse(response.data)
              localStorage.setItem('moban',response.data)
              res('ok')
            })
          })
    }
  })
  p.then(()=>{
    localStorage.setItem('congnalai',from.path)

    if(to.path == '/minishop'){
    	document.title = ''
    }else{
    	if(Vue.prototype.moban){
    		document.title =  '山东广电COMS系统'
    	}else{
    		document.title =  '山东广电COMS系统'
    	}

    }

    if(to.path == '/xinbaobiao'){
      if(localStorage.getItem('USER')){
        let x = 0
        JSON.parse(localStorage.getItem('USER')).functions.forEach(item=>{
          if(item.code == 'S'){
            x = 1
          }
        })
        if(x == 1){
          next()
        }else{
          next('/login')
        }
      }else{
        next('/login')
      }
    }


    /* 路由发生变化修改页面title */
    if (to.path == '/admindingdan' || to.path == 'adminshangjia') {
      if(JSON.parse(localStorage.getItem('SHOP')).shop_id  == 50481 || JSON.parse(localStorage.getItem('SHOP')).shop_id == 301655){
    	  next()
      }else{
    	  next('/login')
      }
    }else{
      next()
    }
  })






})

function dispatchEventStroage () {
  const signSetItem = localStorage.setItem
  localStorage.setItem = function (key, val) {
      let setEvent = new Event('setItemEvent')
      setEvent.key = key
      setEvent.newValue = val
      window.dispatchEvent(setEvent)
      signSetItem.apply(this, arguments)
  }
}

Vue.use(dispatchEventStroage)


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
